﻿using ORM.Enum;
using ORM.Exception;
using ORM.Model;
//using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ORM.DB
{
    public class DBManage
    {
        private  const string _MYSQ_PROVIDENAME = "MySql.Data.MySqlClient";
        private  const string _ORACLE_PROVIDENAME = "System.Data.OracleClient";
        private  const string _SQLSERVER_PROVIDENAME = "System.Data.SqlClient";
        public DBType GetDBType(string ConnectionStringName)
        {
            DBType dbtype = DBType.NoSupportDB;
            string ProviderName = System.Configuration.ConfigurationManager.ConnectionStrings[ConnectionStringName].ProviderName;
            switch (ProviderName)
            {
                case _MYSQ_PROVIDENAME:
                    dbtype = DBType.MySQL;
                    break;
                case _ORACLE_PROVIDENAME:
                    dbtype = DBType.Oracle;
                    break;
                case _SQLSERVER_PROVIDENAME:
                    dbtype = DBType.SQLServer;
                    break;
            }
            if (dbtype != DBType.NoSupportDB)
            {
                return dbtype;
            }
            else
            {
                throw new NoSupportDBTypeException();
            }
        }

        public DbConnection GetDBConn(string ConnectionString, DBType @DBType)
        {
            DbConnection Conn = null;
            switch (DBType)
            {
                case DBType.SQLServer:
                    Conn = new SqlConnection(ConnectionString);
                    break;
                case DBType.Oracle:
                    Conn = new OracleConnection(ConnectionString);
                    break;
                case DBType.MySQL:
                    //Conn = new MySqlConnection(ConnectionString);
                    break;

            }
            return Conn;
        }

        public DbCommand GetDBCommand(DbConnection Conn, String SQL, DBType @DBType)
        {
            DbCommand Command = null;
            switch (DBType)
            {
                case DBType.SQLServer:
                    Command = new SqlCommand(SQL, (SqlConnection)Conn);
                    break;
                case DBType.Oracle:
                    Command = new OracleCommand(SQL, (OracleConnection)Conn);
                    break;
                case DBType.MySQL:
                    //Command = new MySqlCommand(SQL, (MySqlConnection)Conn);
                    break;

            }
            return Command;
        }

        public DbDataAdapter GetDBAdapter(DbCommand Command, DBType @DBType)
        {
            DbDataAdapter Adapter = null;
            switch (DBType)
            {
                case DBType.SQLServer:
                    Adapter = new SqlDataAdapter((SqlCommand)Command);
                    break;
                case DBType.Oracle:
                    Adapter = new OracleDataAdapter((OracleCommand)Command);
                    break;
                case DBType.MySQL:
                    //Adapter = new MySqlDataAdapter((MySqlCommand)Command);
                    break;

            }
            return Adapter;
        }

        public void AddParmas(DbCommand Command, Param _Param, DBType @DBType)
        {
            switch (DBType)
            {
                case DBType.SQLServer:
                    ((SqlCommand)Command).Parameters.AddWithValue(_Param.ParamName, _Param.ParamValue);
                    break;
                case DBType.Oracle:
                    ((OracleCommand)Command).Parameters.AddWithValue(_Param.ParamName, _Param.ParamValue);
                    break;
                case DBType.MySQL:
                    //((MySqlCommand)Command).Parameters.AddWithValue(_Param.ParamName, _Param.ParamValue);
                    break;

            }
        }


    }
}
